﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Localizer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadFolder(treeView1.Nodes[0],"d:/project");
        }

        public void Load(string filename)
        {
            textDoc.Clear();

            try
            {
                StreamReader reader = new StreamReader(filename);

            int[,] strings = new int[2,100];

            int pos = 0;
            int k = 0;
            while(true)
            {
                string line = reader.ReadLine();
                if (line == null)
                    break;
                int pos1 = 0;
                int pos2 = -1;
                textDoc.AppendText(line);
                textDoc.AppendText("\n");
                while (true)
                {
                    pos1 = line.IndexOf("\"", pos2 +1);
                    if (pos1 < 0)
                        break;
                    pos2 =  line.IndexOf("\"", pos1 +1);
                    strings[0,k] = pos1 + pos +1;
                    strings[1,k] = pos2 + pos;
                    k++;
                }
                pos += line.Length;
                pos += 1;
            }

            for (int i = 0; i < k; i++)
            {
                textDoc.SelectionStart = strings[0,i];
                textDoc.SelectionLength = strings[1,i] - strings[0,i];
                textDoc.SelectionColor = Color.White;
                textDoc.SelectionBackColor = Color.Blue;
            }

            }
            catch (Exception e)
            {
                return;
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            Load(treeView1.SelectedNode.Name);
        }

        private void LoadFolder(TreeNode folder, string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            TreeNode node = new TreeNode();
            node.Text = dir.Name;
            folder.Nodes.Add(node);

            foreach (FileInfo file in dir.GetFiles("*.cs"))
            {
                TreeNode node2 = new TreeNode();
                node2.Text = file.Name;
                node2.Name = file.FullName;
                node.Nodes.Add(node2);
            }

            foreach (DirectoryInfo dir2 in dir.GetDirectories())
                LoadFolder(node, dir2.FullName);
        }

        private void OnSelectChanged(object sender, TreeViewEventArgs e)
        {
            Load(treeView1.SelectedNode.Name);
        }
    }
}
